<template>
  <div>
    <el-dialog title="创建菜单" :visible="dialogVisible" @close="handelClose">
      <el-form
        :rules="ruleInline"
        ref="formMenu"
        :model="formMenu"
        label-position="left"
        label-width="120px"
        style="width: 400px; margin-left: 120px"
      >
        <el-form-item label="权限组名称">
          <el-radio-group v-model="formMenu.radio">
            <el-radio label="1">菜单</el-radio>
            <el-radio label="2">权限点</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="权限组名称">
          <el-select v-model="formMenu.pid" no-data-text="主导航">
            <el-option v-for="item in list" :key="item.id" :label="item.title"  :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限代码" prop="code">
          <el-input v-model="formMenu.code"></el-input>
        </el-form-item>
        <el-form-item label="权限标题" prop="title">
          <el-input v-model="formMenu.title"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handelClose">取消</el-button>
        <el-button type="primary" @click="addComfirm">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, list } from '@/api/base/menus'
export default {
  name: 'menuAdd',
  components: {},
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formMenu: {
        pid: '', // 父级Id
        is_point: '', // 是否权限点
        code: '', // 菜单代码
        title: '' // 标题
      },
      ruleInline: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '代码不能为空', trigger: 'blur' }]
      },
      list: []
    }
  },
  computed: {},
  watch: {
    dialogVisible (val) {
      if (val) {
        const { data } = list()
        this.list = data
      }
    }
  },
  created () {},
  methods: {
    async handelClose () {
      await this.$refs.formMenu.resetFields()
      this.$emit('update:dialogVisible', false)
      this.formMenu = {
        pid: '', // 父级Id
        is_point: '', // 是否权限点
        code: '', // 菜单代码
        title: '' // 标题
      }
    },
    async addComfirm () {
      await this.$refs.formMenu.validate()
      await add(this.formMenu)
      this.$message.success('添加成功')
      this.handelClose()
    }
  }
}
</script>

<style scoped>
</style>
